package scsc.web.system.actions;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.taskmgmt.def.TaskMgmtDefinition;

import scsc.pm.SysOperation;
import scsc.pub.data.UserContext;
import scsc.pub.db.DBManage;
import scsc.pub.db.PageListData;
import scsc.pub.exception.UnifyUserException;
import scsc.pub.tools.StringUtil;
import scsc.web.system.forms.SysOperatorForm;
import scsc.web.system.forms.TaskForm;

public class SelectTaskAction extends Action {
	public SelectTaskAction() {
		super();
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException, UnifyUserException {

		TaskForm actionForm = (TaskForm) form;
		String fwd = null;
		ActionErrors errors = new ActionErrors();

		HttpSession session = request.getSession();
		UserContext userContext = (UserContext) session
				.getAttribute("userContext");

		String selectName = StringUtil.trim(actionForm.getSelectName());
		String selectProcess = StringUtil.trim(actionForm.getSelectProcess());

		JbpmContext jbpmContext = JbpmContext.getCurrentJbpmContext();
		GraphSession graphSession = jbpmContext.getGraphSession();
		List processList = graphSession.findLatestProcessDefinitions();
		request.setAttribute("processList", processList);
		PageListData footer = actionForm.getFooter();
		if (!selectProcess.equals("")) {
			TaskMgmtDefinition taskMgmt = graphSession
					.findLatestProcessDefinition(selectProcess)
					.getTaskMgmtDefinition();
			Map task=taskMgmt.getTasks();
			DBManage.sqlQueryPage(task.values(),footer);
		}
		fwd = "selectTask";

		// return data from database and set foreign key list
		// setGroupList(request, userContext, groupLocal, errors);

		if (!errors.isEmpty()) {
			saveErrors(request, errors);
		}

		return mapping.findForward(fwd);
	}
}
